package com.zmc.product.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmc.common.utils.StringUtils;
import com.zmc.product.infrastructure.bean.vo.PropertyValueVo;
import com.zmc.product.infrastructure.service.CommodityCategoryPropertyValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zmc.product.infrastructure.dao.CommodityGoodsPropertyValueDao;
import com.zmc.product.infrastructure.entity.CommodityGoodsPropertyValueEntity;
import com.zmc.product.infrastructure.service.CommodityGoodsPropertyValueService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("commodityGoodsPropertyValueService")
public class CommodityGoodsPropertyValueServiceImpl extends ServiceImpl<CommodityGoodsPropertyValueDao, CommodityGoodsPropertyValueEntity> implements CommodityGoodsPropertyValueService {

    @Autowired
    private CommodityCategoryPropertyValueService categoryPropertyValueService;


    @Override
    public List<CommodityGoodsPropertyValueEntity> getListBySpuId(String spuId) {
        LambdaQueryWrapper<CommodityGoodsPropertyValueEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityGoodsPropertyValueEntity::getSpuId, spuId);
        wrapper.orderByAsc(CommodityGoodsPropertyValueEntity::getOrderNum);
        return list(wrapper);
    }

    @Override
    public void removeBySpuId(String spuId) {
        baseMapper.removeBySpuId(spuId);
    }

    @Override
    @Transactional
    public void savePropertyValues(Long categoryId, List<String> spuList, String spuId) {
        List<PropertyValueVo> valueVoList = categoryPropertyValueService.getGoodsSpuPropertyValueListByCategoryIdAndType(categoryId);
        List<CommodityGoodsPropertyValueEntity> propertyValueEntityList = new ArrayList<>();
        for (int i = 0; i < spuList.size(); i++) {
            if (StringUtils.isNotEmpty(spuList.get(i))) {
                PropertyValueVo valueVo = valueVoList.get(i);
                if ("0".equals(valueVo.getPropertyValueId())) {
                    break;
                }
                CommodityGoodsPropertyValueEntity propertyValueEntity = new CommodityGoodsPropertyValueEntity();
                propertyValueEntity.setCategoryPropertyValueId(Long.valueOf(valueVo.getPropertyValueId()));
                propertyValueEntity.setSpuId(spuId);
                propertyValueEntity.setPropertyValue(spuList.get(i));
                propertyValueEntity.setOrderNum(i);
                propertyValueEntityList.add(propertyValueEntity);
            }
        }
       saveBatch(propertyValueEntityList);
    }

    @Override
    public List<String> setPropertyValue(String spuId, Long categoryId) {
        List<PropertyValueVo> valueVoList = categoryPropertyValueService.getGoodsSpuPropertyValueListByCategoryIdAndType(categoryId);
        List<CommodityGoodsPropertyValueEntity> spuList = getListBySpuId(spuId);
        List<String> spuValueList = new ArrayList<>();
        for (PropertyValueVo propertyValueVo : valueVoList) {
            String tmp = "";
            for (CommodityGoodsPropertyValueEntity propertyValueEntity : spuList) {
                if (Objects.equals(propertyValueVo.getPropertyValueId(), propertyValueEntity.getCategoryPropertyValueId().toString())) {
                    tmp = propertyValueEntity.getPropertyValue();
                }
            }
            spuValueList.add(tmp);
        }
        return spuValueList;
    }

}